﻿using MxNetwork;
using System;
using System.Collections.Generic;

namespace DTFight
{
	public class RoomMsg
	{
		static RoomMsg	mInstance = new RoomMsg();
		static public RoomMsg Instance()	{ return mInstance; }
		
		public void Init()
		{
			NetworkDispatcher.Instance.AddParser ((int)MSG_COMMON.ERROR, OnParseError);

			NetworkDispatcher.Instance.AddParser ((int)MSG_ROOM.ENTER_ROOM, OnParseEnterRoom);
			NetworkDispatcher.Instance.AddParser ((int)MSG_ROOM.EXIT_ROOM, OnParseExitRoom);

			NetworkDispatcher.Instance.AddParser ((int)MSG_ROOM.GAME_START, OnParseGameStart);
			NetworkDispatcher.Instance.AddParser ((int)MSG_ROOM.GAME_SHOW_CARD, OnParseGameShowCard);
			NetworkDispatcher.Instance.AddParser ((int)MSG_ROOM.GAME_OVER, OnParseGameOver);

			NetworkDispatcher.Instance.AddParser ((int)MSG_ROOM.SELECT_ADVISER, OnParseSelectAdviser);
			NetworkDispatcher.Instance.AddParser ((int)MSG_ROOM.SELECT_BET_PLAYER, OnParseSelectBetPlayer);
			NetworkDispatcher.Instance.AddParser ((int)MSG_ROOM.SELECT_GROUP, OnParseSelectGroup);
			NetworkDispatcher.Instance.AddParser ((int)MSG_ROOM.BET_POINT, OnParseBetPoint);
			NetworkDispatcher.Instance.AddParser ((int)MSG_ROOM.BET_RESULT, OnParseBetResult);
		}
		
		public void Destroy()
		{
			NetworkDispatcher.Instance.RemoveParser ((int)MSG_COMMON.ERROR, OnParseError);

			NetworkDispatcher.Instance.RemoveParser ((int)MSG_ROOM.ENTER_ROOM, OnParseEnterRoom);
			NetworkDispatcher.Instance.RemoveParser ((int)MSG_ROOM.EXIT_ROOM, OnParseExitRoom);
			
			NetworkDispatcher.Instance.RemoveParser ((int)MSG_ROOM.GAME_START, OnParseGameStart);
			NetworkDispatcher.Instance.RemoveParser ((int)MSG_ROOM.GAME_SHOW_CARD, OnParseGameShowCard);
			NetworkDispatcher.Instance.RemoveParser ((int)MSG_ROOM.GAME_OVER, OnParseGameOver);

			NetworkDispatcher.Instance.RemoveParser ((int)MSG_ROOM.SELECT_ADVISER, OnParseSelectAdviser);
			NetworkDispatcher.Instance.RemoveParser ((int)MSG_ROOM.SELECT_BET_PLAYER, OnParseSelectBetPlayer);
			NetworkDispatcher.Instance.RemoveParser ((int)MSG_ROOM.SELECT_GROUP, OnParseSelectGroup);
			NetworkDispatcher.Instance.RemoveParser ((int)MSG_ROOM.BET_POINT, OnParseBetPoint);
			NetworkDispatcher.Instance.RemoveParser ((int)MSG_ROOM.BET_RESULT, OnParseBetResult);
		}

		void OnParseError(MxNwkPacket pkt, MxNwkPacket pkt_rq)
		{
			
		}
		
		// ENTER_ROOM
		public void SubmitEnterRoom(string playerId, int roomId)
		{		
			MxNwkPacket req = new MxNwkPacket(MxNwkPacket.MxNwkPacketType.NPT_REQUEST);
			req.SetPacketID((uint)MSG_ROOM.ENTER_ROOM);
			req.Write(playerId);
			req.Write(roomId);
			NetworkDispatcher.Instance.SendRequestToServer(req);
		}
		
		void OnParseEnterRoom(MxNwkPacket pkt, MxNwkPacket pkt_rq)
		{
			if (pkt.GetPacketResult() != 0)
			{
				return;
			}

			int roomId = pkt_rq.ReadInt32();

			if (UIGameMgr.CurRoom == null) {
				UIGameMgr.CurRoom = new GameData ();
				UIGameMgr.CurRoom.Init();
			}

			for (int i=0; i<GameData.WaiterTime.Length; ++i) {
				GameData.WaiterTime[i] = pkt.ReadFloat();
			}

			GameData.Write (ref pkt, ref UIGameMgr.CurRoom);

			if (UIGameMgr.CurPlayer == null)
				UIGameMgr.CurPlayer = new PlayerData ();

			UIGameMgr.CurPlayer.PlayerId = pkt.ReadString ();
			UIGameMgr.CurPlayer.Points = pkt.ReadUInt32 ();
			UIGameMgr.CurPlayer.CurRoomId = roomId;

			UILogin.CurPlayer.Points = (int)UIGameMgr.CurPlayer.Points;

			UILoading.Instance.LoadScene("Ready", false);
		}

		// EXIT_ROOM
		public void SubmitExitRoom()
		{		
			MxNwkPacket req = new MxNwkPacket(MxNwkPacket.MxNwkPacketType.NPT_REQUEST);
			req.SetPacketID((uint)MSG_ROOM.EXIT_ROOM);
			NetworkDispatcher.Instance.SendRequestToServer(req);
		}
		
		void OnParseExitRoom(MxNwkPacket pkt, MxNwkPacket pkt_rq)
		{
			if (pkt.GetPacketResult() != 0)
			{
				return;
			}

			UILoading.Instance.LoadScene("RoomList", false);
		}

		void OnParseGameStart(MxNwkPacket pkt, MxNwkPacket pkt_rq)
		{
			if (pkt.GetPacketResult() != 0)
			{
				return;
			}

			UIGameMgr.CurRoom.Start();
			UIGameMgr.CurGroup = GameData.GROUP_TYPE.MAX;
			UIGameMgr.AdviserAnwser = GameData.GROUP_TYPE.MAX;

			for (int i=0; i<UIGameMgr.CurPlayer.BetPoints.Length; ++i) {
				UIGameMgr.CurPlayer.BetPoints [i] = 0;
			}

			if (UIGameMgr.Instance != null)
				UIGameMgr.Instance.ShowResultTip();
		}
		
		void OnParseGameShowCard(MxNwkPacket pkt, MxNwkPacket pkt_rq)
		{
			if (pkt.GetPacketResult() != 0)
			{
				return;
			}

			GameData room = UIGameMgr.CurRoom;
			room.CurStateInfo.StateFlag = (GameData.STATE_FLAG)pkt.ReadUInt32 ();
			room.CurStateInfo.StartTime = DateTime.Now;

			for (int i=0; i<room.DesktopCards.Length; ++i) {
				byte cardId = pkt.ReadUInt8();
				if (cardId == 0)
					room.DesktopCards[i] = null;
				else
					room.DesktopCards[i] = CardData.CardPool[cardId - 1];
			}

			if (UIGameMgr.Instance != null)
				UIGameMgr.Instance.ShowResultTip();
		}

		void OnParseGameOver(MxNwkPacket pkt, MxNwkPacket pkt_rq)
		{
			if (pkt.GetPacketResult() != 0)
			{
				return;
			}

			GameData.ResultInfo result = new GameData.ResultInfo ();
			GameData.ResultInfo.Write(ref pkt, ref result);

			UIGameMgr.CurRoom.AddResultInfo(result);

			GameData room = UIGameMgr.CurRoom;
			room.CurStateInfo.StateFlag = GameData.STATE_FLAG.SHOW_BET_RESULT;
			room.CurStateInfo.StartTime = DateTime.Now;
			room.CurStateInfo.WaiteTime = GameData.WaiterTime[(int)GameData.STATE_FLAG.SHOW_BET_RESULT];

			if (UIGameMgr.Instance != null)
				UIGameMgr.Instance.ShowGameResult (result);
		}

		// SELECT_ADVISER
		public void SubmitSelectAdviser(GameData.ADVISER_TYPE adviser)
		{		
			MxNwkPacket req = new MxNwkPacket(MxNwkPacket.MxNwkPacketType.NPT_REQUEST);
			req.SetPacketID((uint)MSG_ROOM.SELECT_ADVISER);
			req.Write ((UInt32)adviser);
			NetworkDispatcher.Instance.SendRequestToServer(req);
		}
		
		void OnParseSelectAdviser(MxNwkPacket pkt, MxNwkPacket pkt_rq)
		{
			if (pkt.GetPacketResult() != 0)
			{
				return;
			}
			
			UIGameMgr.AdviserAnwser = (GameData.GROUP_TYPE)pkt.ReadUInt32 ();
			UIGameMgr.CurPlayer.Points = pkt.ReadUInt32();

			UILogin.CurPlayer.Points = (int)UIGameMgr.CurPlayer.Points;
		}

		// SELECT_BET_PLAYER
		public void SubmitSelectBetPlayer(string playerId)
		{		
			MxNwkPacket req = new MxNwkPacket(MxNwkPacket.MxNwkPacketType.NPT_REQUEST);
			req.SetPacketID((uint)MSG_ROOM.SELECT_BET_PLAYER);
			req.Write (playerId);
			NetworkDispatcher.Instance.SendRequestToServer(req);
		}
		
		void OnParseSelectBetPlayer(MxNwkPacket pkt, MxNwkPacket pkt_rq)
		{
			if (pkt.GetPacketResult() != 0)
			{
				return;
			}
			
			//GameData.GROUP_TYPE group = (GameData.GROUP_TYPE)pkt.ReadUInt32 ();
			UIGameMgr.CurPlayer.Points = pkt.ReadUInt32();
			//UIGameMgr.Instance.SelectGroup (group);

			UILogin.CurPlayer.Points = (int)UIGameMgr.CurPlayer.Points;
		}

		// SELECT_GROUP
		public void SubmitSelectGroup(GameData.GROUP_TYPE group)
		{		
			MxNwkPacket req = new MxNwkPacket(MxNwkPacket.MxNwkPacketType.NPT_REQUEST);
			req.SetPacketID((uint)MSG_ROOM.SELECT_GROUP);
			req.Write ((UInt32)group);
			NetworkDispatcher.Instance.SendRequestToServer(req);
		}
		
		void OnParseSelectGroup(MxNwkPacket pkt, MxNwkPacket pkt_rq)
		{
			if (pkt.GetPacketResult() != 0)
			{
				return;
			}

			GameData.GROUP_TYPE group = (GameData.GROUP_TYPE)pkt_rq.ReadUInt32 ();
			UIGameMgr.CurPlayer.Points = pkt.ReadUInt32();
			UIGameMgr.Instance.SelectGroup (group);

			UILogin.CurPlayer.Points = (int)UIGameMgr.CurPlayer.Points;
		}

		// BET_POINT
		public void SubmitBetPoint(UInt32 points)
		{		
			MxNwkPacket req = new MxNwkPacket(MxNwkPacket.MxNwkPacketType.NPT_REQUEST);
			req.SetPacketID((uint)MSG_ROOM.BET_POINT);
			req.Write (points);
			NetworkDispatcher.Instance.SendRequestToServer(req);
		}
		
		void OnParseBetPoint(MxNwkPacket pkt, MxNwkPacket pkt_rq)
		{
			if (pkt.GetPacketResult() != 0)
			{
				return;
			}

			UInt32 points = pkt_rq.ReadUInt32 ();

			byte rate = pkt.ReadUInt8 ();
			UIGameMgr.CurPlayer.BetPoints [rate - 1] = pkt.ReadUInt32 ();
			UIGameMgr.CurPlayer.Points = pkt.ReadUInt32 ();

			UILogin.CurPlayer.Points = (int)UIGameMgr.CurPlayer.Points;
		}

		void OnParseBetResult(MxNwkPacket pkt, MxNwkPacket pkt_rq)
		{
			UIGameMgr.CurPlayer.Points = pkt.ReadUInt32 ();			
			UInt32 winPoints = pkt.ReadUInt32();
			UIGameMgr.Instance.ShowBetResult (true, winPoints);

			UILogin.CurPlayer.Points = (int)UIGameMgr.CurPlayer.Points;
		}
	}
}

